/**************************************************************************************************
* Title: SumMeshLoader.h
* Author: Gael Huber
* Description: Loads 3DS Max .3ds mesh files for use within SumEngine.
**************************************************************************************************/
#ifndef __SUMMeshLoader_H__
#define __SUMMeshLoader_H__

#include <fstream>
#include "SumSingleton.h"
#include "SumString.h"
#include "SumMesh.h"
#include "SumFileLoading.h"
#include "SumRenderManager.h"

using namespace std;

class MeshLoader : public Singleton<MeshLoader> {
private:
	// Seek base locations
	enum SeekOrigin {SEEK_FSTART = 0, SEEK_FCURRENT, SEEK_FEND };

public:
	MeshLoader(void);	// Constructor
	virtual ~MeshLoader(void);	// Destructor

	// Get singleton
	inline static MeshLoader& getSingleton(void)
	{
		assert(singleton);
		return *singleton;
	}
	// Get singleton pointer
	inline static MeshLoader* getSingletonPtr(void)
	{
		return singleton;
	}

	void load(String filepath);		// Load a mesh from file

private:
	// Read a character as an unsigned integer
	inline unsigned char readUCHAR(FILE* file)
	{
		return fgetc(file);
	}

	// Convert a character to an unsigned integer
	inline unsigned int readUINT(FILE* file)
	{
		return (unsigned int)readUCHAR(file) | (unsigned int)readUCHAR(file) << 8;
	}

	// Read an unsigned long
	inline unsigned long readULONG(FILE* file) 
	{
		return (unsigned long)readUINT(file) | (unsigned long)readUINT(file) << 16;
	}

	// Read a float
	inline float readFLOAT(FILE* file) {
		float f;
		fread(&f, sizeof(float), 1, file);
		return f;
	}
	
	int readChunk(FILE* file);		// Read a chunk of data from the file
	void readObject(FILE* file, unsigned long len);		// Read the object from the file
	void readMesh(FILE* file, unsigned long len);		// Read mesh data
	void readMaterial(FILE* file, unsigned long len);		// Read the material from the file

private:
	Mesh* mesh;

};

#endif